<?php


namespace app\api\controller;


use app\common\model\AdminAdmin;
use app\common\model\AdminRole;
use app\common\model\PurchaseGoods;
use app\common\model\PurchaseGoodsOut;
use app\common\model\SchoolPettyCash;

class Finance
{
    /**
     * 备用金申请
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pettyCash()
    {
        $id = input('param.id');
        $admin = AdminAdmin::where('id', $id)->find();
        $where[] = ['principal_audit', '=', 2];
        if ($admin['school_id'] > 0) {
            $where[] = ['school_id', '=', $admin['school_id']];
        }
        $list = SchoolPettyCash::with([
            'get_admin' => function($a) {
                $a->field('id,username');
            },'get_school' => function($a) {
                $a->field('id,school_name');
            }
        ])->order('id','desc')->where($where)->select();
        foreach ($list as $k => $v) {
            $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc');
            if ($users) {
                $items[$k]['group'] = AdminRole::where('id', $users['role_id'])->value('name');
            } else {
                $items[$k]['group'] = '未分组';
            }
        }
        resReturn($list);
    }

    /**
     * 申请物品列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function goodsList()
    {
        $id = input('param.id');
        $admin = AdminAdmin::where('id', $id)->find();
        $goods = PurchaseGoods::field('id,goods_name')
            ->where('school_id', $admin['school_id'])
            ->where('status', 1)
            ->select();
        resReturn($goods);
    }

    /**
     * 物资申请
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function goodsApply()
    {
        $data = input('');
        $admin = AdminAdmin::where('id', $data['admin_id'])->find();
        $data['apply_id'] = 0;
        $data['apply_info'] = '';
        $data['school_id'] = $admin['school_id'];
        $data['add_time'] = time();
        PurchaseGoodsOut::create($data)  ? resReturn('', '提交成功', 200) : resReturn('', '提交失败', 101);
    }

    /**
     * 申请历史
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function goodsApplyHistory()
    {
        $admin_id = input('param.admin_id');
        $list = PurchaseGoodsOut::with([
            'get_goods' => function($a) {
                $a->field('id,goods_name');
            },
            'get_admin' => function($a) {
                $a->field('id,username');
            },
            'get_apply' => function($a) {
                $a->field('id,username');
            }
        ])->where('admin_id', $admin_id)
            ->order('id desc')
            ->select();
        resReturn($list);
    }

}